﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Entidades.Generico;

namespace Datos.Generico
{
    public class GenUsuarioDAL
    {
        public static string cnx = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();



        public static GenUsuario ObtenerUsuario(string usuario)
        {

            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand("[dbo].[SP_KAR_OBTENER_USUARIO]", con);
            com.Parameters.AddWithValue("@usuario", usuario);

            com.CommandType = System.Data.CommandType.StoredProcedure;
            try
            {
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                GenUsuario obj = null;
                if (dr.Read())
                {
                    obj = new GenUsuario();
                    obj.Usuario = Convert.ToString(dr["usuario"].ToString());
                    obj.Localidad = Convert.ToString(dr["localidad"].ToString());
                    obj.Perfil = Convert.ToString(dr["perfil"].ToString());
                    obj.Nombre = Convert.ToString(dr["nombre"].ToString());
                }
                dr.Close();

                return obj;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }


        public static bool Autenticar(string usuario, string password)
        {

            string sql = @"SELECT COUNT(*)
                       FROM Gen_Usuario
                       WHERE codUsuario = @usuario AND password = @password";

            SqlConnection con = new SqlConnection(cnx);
            SqlCommand com = new SqlCommand(sql, con);
            com.CommandType = System.Data.CommandType.Text;

            com.Parameters.AddWithValue("@usuario", usuario);
            com.Parameters.AddWithValue("@password", password);
            try
            {
                con.Open();
                int count = Convert.ToInt32(com.ExecuteScalar());

                if (count == 0)
                    return false;
                else
                    return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }

    }
}
